﻿Imports System.Globalization.CultureInfo

Public Class fZadaniVydajePolozkyDialog
    'counter of no. of clicks on btn "Další"
    Dim pocetKliknutiBtnDalsi As Long = 0
    'counteer of no. of rows in tables
    Dim pocetRadkuVtab As Long = 0
    'vars for table id's
    Dim id_faktura As Long = 0
    Dim id_platby As Long = 0
    Dim id_dodavatel As Long = 0
    Dim id_vstup() As Long
    Dim id_postovne As Long = 0
    Dim id_poznamky As Long = 0


    Private Sub fZadaniVydajePolozkyDialog_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        My.Forms.fZadaniVydaje.Dispose()
        My.Forms.fZadaniVydaje.Close()
        Me.Dispose()
    End Sub

    Private Sub fZadaniVydajePolozkyDialog_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'loads needed dtb content
        Me.Seznam_VydajFakturyTableAdapter.Fill(Me.DtbDataSet.seznam_VydajFaktury)
        Me.Seznam_VydajDodavatelTableAdapter.Fill(Me.DtbDataSet.seznam_VydajDodavatel)
        Me.Tab_vydajTableAdapter.Fill(Me.DtbDataSet.tab_vydaj)
        Me.Seznam_poznamkyTableAdapter1.Fill(Me.DtbDataSet.seznam_poznamky)
        Me.Seznam_PrijemPostovneTableAdapter1.Fill(Me.DtbDataSet.seznam_PrijemPostovne)
        Me.Seznam_PrijemTypPlatbyTableAdapter1.Fill(Me.DtbDataSet.seznam_PrijemTypPlatby)
        Me.Seznam_vstupyTableAdapter1.Fill(Me.DtbDataSet.seznam_vstupy)
        '-----------------------
        'sets dimension of public array
        ReDim _zadaniVydajePolozky(My.Forms.fZadaniVydaje.chboxFaktVyrobky.CheckedItems.Count() - 1, 2)
        'disables btnPredchozi by default
        Me.btnPredchozi.Enabled = False
        'disables btnHotovo by default, except when checked products equal to one
        If My.Forms.fZadaniVydaje.chboxFaktVyrobky.CheckedItems.Count() = 1 Then
            Me.btnHotovo.Enabled = True
            Me.btnDalsi.Enabled = False
        Else
            Me.btnHotovo.Enabled = False
        End If
        'sets numBox maximum value
        Me.numCheckMnozstvi.Maximum = 9999
        'loads 1st selected input's name into label
        Me.lblNazevVstupuShow.Text = My.Forms.fZadaniVydaje.chboxFaktVyrobky.CheckedItems.Item(0)
        '


    End Sub

    Private Sub btnDalsi_Click(sender As System.Object, e As System.EventArgs) Handles btnDalsi.Click
        If Me.tboxCenaKus.Text.Length > 0 And Me.numCheckMnozstvi.Value > 0 Then
            Try ' return previous entry, if exists
                If _zadaniVydajePolozky(Me.pocetKliknutiBtnDalsi + 1, 0) <> "" Then
                    Me.pocetKliknutiBtnDalsi += 1
                    Me.btnPredchozi.Enabled = True
                    Me.lblNazevVstupuShow.Text = _zadaniVydajePolozky(Me.pocetKliknutiBtnDalsi, 0)
                    Me.numCheckMnozstvi.Value = _zadaniVydajePolozky(Me.pocetKliknutiBtnDalsi, 1)
                    Me.tboxCenaKus.Text = Convert.ToString(_zadaniVydajePolozky(Me.pocetKliknutiBtnDalsi, 2))
                    Me.tboxCelkem.Text = _
                        Convert.ToString(_zadaniPrijmyPolozky(Me.pocetKliknutiBtnDalsi, 1) * _zadaniVydajePolozky(Me.pocetKliknutiBtnDalsi, 2))
                    'saves new entry into public array and prepares form for next filling
                ElseIf Me.pocetKliknutiBtnDalsi < _zadaniVydajePolozky.GetLength(0) - 1 Then
                    Fill_zadaniVydajePolozky( _
                        Me.lblNazevVstupuShow.Text, _
                        Me.numCheckMnozstvi.Value, _
                        Convert.ToDecimal(Trim(Me.tboxCenaKus.Text)), _
                        Me.pocetKliknutiBtnDalsi)
                    Me.btnPredchozi.Enabled = True
                    Me.pocetKliknutiBtnDalsi += 1
                    Me.lblNazevVstupuShow.Text = My.Forms.fZadaniVydaje.chboxFaktVyrobky.CheckedItems.Item( _
                        Me.pocetKliknutiBtnDalsi)
                    Me.numCheckMnozstvi.Value = 0
                    Me.tboxCenaKus.Text = ""
                    Me.tboxCelkem.Text = ""
                End If
                If Me.pocetKliknutiBtnDalsi = _zadaniVydajePolozky.GetLength(0) - 1 Then
                    Me.btnDalsi.Enabled = False
                    Me.btnHotovo.Enabled = True
                End If
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical, "Chyba při zpracování další položky!")
            End Try
        Else
            MsgBox("Je třeba vyplnit všechny položky!", MsgBoxStyle.Exclamation, "Chyba!")
        End If
    End Sub

    Private Sub tboxCenaKus_LostFocus(sender As Object, e As System.EventArgs) Handles tboxCenaKus.LostFocus
        If Me.tboxCenaKus.Text.Contains(",") = True Then
            Me.tboxCenaKus.Text = Me.tboxCenaKus.Text.Replace(",", ".")
        End If
        Me.tboxCelkem.Text = _
            Convert.ToString(Convert.ToDecimal(Me.tboxCenaKus.Text) * Me.numCheckMnozstvi.Value)

    End Sub

    Private Sub btnPredchozi_Click(sender As System.Object, e As System.EventArgs) Handles btnPredchozi.Click
        Try 'if previously saved data exists in array, displays them
            If Me.pocetKliknutiBtnDalsi > 0 Then
                Me.pocetKliknutiBtnDalsi -= 1
                Me.lblNazevVstupuShow.Text = _zadaniVydajePolozky(Me.pocetKliknutiBtnDalsi, 0)
                Me.numCheckMnozstvi.Value = _zadaniVydajePolozky(Me.pocetKliknutiBtnDalsi, 1)
                Me.tboxCenaKus.Text = Convert.ToString(_zadaniVydajePolozky(Me.pocetKliknutiBtnDalsi, 2))
                Me.tboxCelkem.Text = Convert.ToString( _
                    _zadaniVydajePolozky(Me.pocetKliknutiBtnDalsi, 1) * _zadaniVydajePolozky(Me.pocetKliknutiBtnDalsi, 2))
                Me.btnDalsi.Enabled = True
                Me.btnHotovo.Enabled = False
                If Me.pocetKliknutiBtnDalsi = 0 Then
                    Me.btnPredchozi.Enabled = False
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Chyba nahrání údajů předchozí položky!")
        End Try
    End Sub

    Private Sub btnHotovo_Click(sender As System.Object, e As System.EventArgs) Handles btnHotovo.Click
        If Me.tboxCenaKus.Text.Length > 0 And Me.numCheckMnozstvi.Value > 0 Then
            Try 'saves last input's data into public array
                Fill_zadaniVydajePolozky( _
                    Me.lblNazevVstupuShow.Text, _
                    Me.numCheckMnozstvi.Value, _
                    Convert.ToDecimal(Trim(Me.tboxCenaKus.Text)), _
                    Me.pocetKliknutiBtnDalsi)
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical, "Chyba při ukládání poslední položky!")
                My.Forms.fMain.lblTStrip1.Text = NEULOZENO
                GoTo HotovoEnd 'terminates form and flush vars
            End Try
            '--------------------------------------
            '--------processing of dtb's tables
            '--------------------------------------
            '----------processing table "seznam_PrijemTypPlatby" (can be used in "Vydaje" too)
            Me.id_platby = My.Forms.fZadaniVydaje.cboxZpusobPlatby.SelectedValue
            '-----------------end
            '------------------------------------
            '--------processing table "seznam_VydajDodavatel"
            Try
                Dim tabDod As DataTable = Me.DtbDataSet.seznam_VydajDodavatel
                Dim chFO As CheckBox = My.Forms.fZadaniVydaje.chboxFyzOsoba
                Dim adr As TextBox = My.Forms.fZadaniVydaje.tboxAdresa
                Dim jm As TextBox = My.Forms.fZadaniVydaje.tboxJmeno
                Dim prijm As TextBox = My.Forms.fZadaniVydaje.tboxPrijmeni
                Dim nazFir As TextBox = My.Forms.fZadaniVydaje.tboxNazevFirmy
                pocetRadkuVtab = tabDod.Rows.Count()

                If pocetRadkuVtab > 0 Then
                    If chFO.Checked = True Then
                        For i As Long = 0 To pocetRadkuVtab - 1
                            If tabDod.Rows.Item(i).Item("jmeno") = jm.Text.Trim _
                                And tabDod.Rows.Item(i).Item("prijmeni") = prijm.Text.Trim _
                                And tabDod.Rows.Item(i).Item("adresa") = adr.Text.Trim Then
                                Me.id_dodavatel = tabDod.Rows.Item(i).Item(0)
                                Exit For
                            ElseIf i = pocetRadkuVtab - 1 Then
                                Dim newDodRow As DataRow = tabDod.NewRow()
                                Dim cVal As Long = 0
                                For j As Long = 0 To pocetRadkuVtab - 1
                                    Dim num As Long = tabDod.Rows.Item(j).Item(0)
                                    If num > cVal Then
                                        cVal = num
                                    End If
                                Next j
                                newDodRow(0) = cVal + 1

                                id_dodavatel = newDodRow(0)

                                newDodRow(1) = chFO.Checked
                                newDodRow(2) = nazFir.Text.Trim
                                newDodRow(3) = jm.Text.Trim
                                newDodRow(4) = prijm.Text.Trim
                                If adr.Lines.Length = 1 Then
                                    newDodRow(5) = adr.Text.Trim
                                Else
                                    Dim adrText As String = ""
                                    For j As Long = 0 To adr.Lines.GetUpperBound(0)
                                        adrText = adrText & adr.Lines(j).Trim & ","
                                    Next j
                                    newDodRow(5) = adrText.Trim
                                End If
                                tabDod.Rows.Add(newDodRow)
                                Me.Seznam_VydajDodavatelTableAdapter.Update(tabDod)
                            End If
                        Next i
                    Else
                        For i As Long = 0 To pocetRadkuVtab - 1
                            If tabDod.Rows.Item(i).Item("adresa") = adr.Text.Trim _
                                And tabDod.Rows.Item(i).Item("nazev_firmy") = nazFir.Text.Trim Then
                                Me.id_dodavatel = tabDod.Rows.Item(i).Item(0)
                                Exit For
                            ElseIf i = pocetRadkuVtab - 1 Then
                                Dim newDodRow As DataRow = tabDod.NewRow()
                                Dim cVal As Long = 0
                                For j As Long = 0 To pocetRadkuVtab - 1
                                    Dim num As Long = tabDod.Rows.Item(j).Item(0)
                                    If num > cVal Then
                                        cVal = num
                                    End If
                                Next j
                                newDodRow(0) = cVal + 1

                                id_dodavatel = newDodRow(0)

                                newDodRow(1) = chFO.Checked
                                newDodRow(2) = nazFir.Text.Trim
                                newDodRow(3) = jm.Text.Trim
                                newDodRow(4) = prijm.Text.Trim
                                If adr.Lines.Length = 1 Then
                                    newDodRow(5) = adr.Text.Trim
                                ElseIf adr.Lines.Length > 1 Then
                                    Dim adrText As String = ""
                                    For j As Long = 0 To adr.Lines.GetUpperBound(0)
                                        adrText = adrText & adr.Lines(j).Trim & ","
                                    Next j
                                    newDodRow(5) = adrText.Trim
                                Else
                                    newDodRow(5) = "N/A"
                                End If
                                tabDod.Rows.Add(newDodRow)
                                Me.Seznam_VydajDodavatelTableAdapter.Update(tabDod)
                            End If
                        Next i

                    End If
                Else
                    Dim newDodRow As DataRow = tabDod.NewRow()
                    newDodRow(0) = 1

                    id_dodavatel = newDodRow(0)

                    newDodRow(1) = chFO.Checked
                    newDodRow(2) = nazFir.Text.Trim
                    newDodRow(3) = jm.Text.Trim
                    newDodRow(4) = prijm.Text.Trim
                    If adr.Lines.Length = 1 Then
                        newDodRow(5) = adr.Text.Trim
                    Else
                        Dim adrText As String = ""
                        For j As Long = 0 To adr.Lines.GetUpperBound(0)
                            adrText = adrText & adr.Lines(j).Trim & ","
                        Next j
                        newDodRow(5) = adrText.Trim
                    End If
                    tabDod.Rows.Add(newDodRow)
                    Me.Seznam_VydajDodavatelTableAdapter.Update(tabDod)
                End If
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical, "Chyba ukládání dat dodavatele")
                My.Forms.fMain.lblTStrip1.Text = NEULOZENO
                GoTo HotovoEnd
            End Try
            '---
            Try
                'tab "seznam_vstupy"
                Dim tabVstup As DataTable = Me.DtbDataSet.seznam_vstupy
                Dim chboxVyr As CheckedListBox = My.Forms.fZadaniVydaje.chboxFaktVyrobky
                ReDim id_vstup(chboxVyr.CheckedItems.Count() - 1)
                pocetRadkuVtab = tabVstup.Rows.Count()

                For i As Long = 0 To id_vstup.GetUpperBound(0)
                    For j As Long = 0 To pocetRadkuVtab - 1
                        If chboxVyr.CheckedItems.Item(i) = _
                            tabVstup.Rows.Item(j).Item("nazev") Then
                            Me.id_vstup(i) = tabVstup.Rows.Item(j).Item(0)
                            Exit For
                        End If
                    Next j
                Next i
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical, "Chyba ukládání názvu vstupu")
                GoTo HotovoEnd
            End Try
            '---
            'table "seznam_PrijemPostovne"
            Try
                Dim tabPost As DataTable = Me.DtbDataSet.seznam_PrijemPostovne
                Dim post As Decimal = Convert.ToDecimal(My.Forms.fZadaniVydaje.tboxPostovne.Text.Trim)
                pocetRadkuVtab = tabPost.Rows.Count()

                For i As Long = 0 To pocetRadkuVtab - 1
                    If tabPost.Rows.Item(i).Item("postovne_hodnota") = post Then
                        Me.id_postovne = tabPost.Rows.Item(i).Item(0)
                        Exit For
                    ElseIf i = pocetRadkuVtab - 1 Then
                        Dim newPostRow As DataRow = tabPost.NewRow()
                        Dim cVal As Long = 0
                        For j As Long = 0 To pocetRadkuVtab - 1
                            Dim num As Long = tabPost.Rows.Item(j).Item(0)
                            If num > cVal Then
                                cVal = num
                            End If
                        Next j
                        newPostRow(0) = cVal + 1

                        id_postovne = newPostRow(0)

                        newPostRow(1) = post
                        tabPost.Rows.Add(newPostRow)
                        Me.Seznam_PrijemPostovneTableAdapter1.Update(tabPost)

                    End If
                Next i
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical, "Chyba ukládání poštovného")
                GoTo HotovoEnd
            End Try
            '---
            Try
                'table "seznam_poznamka"
                Dim tabPoz As DataTable = Me.DtbDataSet.seznam_poznamky
                Dim pozn As TextBox = My.Forms.fZadaniVydaje.tboxPoznamka
                Dim poznText As String = ""
                pocetRadkuVtab = tabPoz.Rows.Count()

                If pocetRadkuVtab > 0 Then
                    If pozn.Lines.Length = 1 Then
                        poznText = pozn.Text.Trim
                    ElseIf pozn.Lines.Length > 1 Then
                        For i As Long = 0 To pozn.Lines.GetUpperBound(0)
                            poznText = poznText & pozn.Lines(i).Trim & ","
                        Next i
                    Else
                        poznText = ""
                    End If

                    For i As Long = 0 To pocetRadkuVtab - 1
                        If tabPoz.Rows.Item(i).Item("text_poznamky") = poznText Then
                            Me.id_poznamky = tabPoz.Rows.Item(i).Item(0)
                            Exit For
                        ElseIf i = pocetRadkuVtab - 1 Then
                            Dim newPozRow As DataRow = tabPoz.NewRow()
                            Dim cVal As Long = 1
                            For j As Long = 0 To pocetRadkuVtab - 1
                                Dim num As Long = tabPoz.Rows.Item(j).Item(0)
                                If num > cVal Then
                                    cVal = num
                                End If
                            Next j
                            newPozRow(0) = cVal + 1

                            id_poznamky = newPozRow(0)

                            newPozRow(1) = poznText
                            tabPoz.Rows.Add(newPozRow)
                            Me.Seznam_poznamkyTableAdapter1.Update(tabPoz)

                        End If
                    Next i
                Else
                    Dim newPozRow As DataRow = tabPoz.NewRow()
                    newPozRow(0) = 1

                    id_poznamky = newPozRow(0)

                    newPozRow(1) = poznText
                    tabPoz.Rows.Add(newPozRow)
                    Me.Seznam_poznamkyTableAdapter1.Update(tabPoz)
                End If
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical, "Chyba ukládání poznámky")
                GoTo HotovoEnd
            End Try
            '---
            'table "seznam_VydajFaktury"
            Try
                Dim tabFak As DataTable = Me.DtbDataSet.seznam_VydajFaktury
                Dim fakVal As String = My.Forms.fZadaniVydaje.tboxCisloFaktury.Text.Trim
                pocetRadkuVtab = tabFak.Rows.Count()

                Dim newFakRow As DataRow = tabFak.NewRow()
                Dim cVal2 As Long = 0
                For i As Long = 0 To pocetRadkuVtab - 1
                    Dim num As Long = tabFak.Rows.Item(i).Item(0)
                    If num > cVal2 Then
                        cVal2 = num
                    End If
                Next i
                newFakRow(0) = cVal2 + 1

                id_faktura = newFakRow(0)

                newFakRow(1) = fakVal
                tabFak.Rows.Add(newFakRow)
                Me.Seznam_VydajFakturyTableAdapter.Update(tabFak)
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical, "Chyba ukládání čísla faktury")
                GoTo HotovoEnd
            End Try
            '---
            'table "tab_vydaj"
            Try
                Dim tabVyd As DataTable = Me.DtbDataSet.tab_vydaj

                For i As Long = 0 To id_vstup.GetUpperBound(0)
                    pocetRadkuVtab = tabVyd.Rows.Count()
                    Dim cVal As Long = 0
                    For j As Long = 0 To pocetRadkuVtab - 1
                        Dim num As Long = tabVyd.Rows.Item(j).Item(0)
                        If num > cVal Then
                            cVal = num
                        End If
                    Next j
                    Dim newVydRow As DataRow = tabVyd.NewRow()
                    newVydRow(0) = cVal + 1
                    newVydRow(1) = id_faktura
                    newVydRow(2) = id_platby
                    newVydRow(3) = id_dodavatel
                    newVydRow(4) = id_vstup(i)
                    newVydRow(5) = id_postovne
                    newVydRow(6) = id_poznamky
                    newVydRow(7) = My.Forms.fZadaniVydaje.dpickDatNakupu.Value.Date
                    newVydRow(8) = My.Forms.fZadaniVydaje.dpickDatSplatnosti.Value.Date
                    newVydRow(9) = _zadaniVydajePolozky(i, 1)
                    newVydRow(10) = _zadaniVydajePolozky(i, 2)
                    tabVyd.Rows.Add(newVydRow)
                    Me.Tab_vydajTableAdapter.Update(tabVyd)
                Next i
                My.Forms.fMain.lblTStrip1.Text = ULOZENO
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical, "Chyba ukládání do tabulky výdajů")
                'rollback of table "seznam_VydajFaktury"
                Dim delRow As DataRow = Me.DtbDataSet.tab_vydaj.Rows.Find(id_faktura)
                Me.DtbDataSet.tab_vydaj.Rows.Remove(delRow)
                Me.Tab_vydajTableAdapter.Update(Me.DtbDataSet.tab_vydaj)

                GoTo HotovoEnd
            End Try



            '---------------end
            '--------------------
            '----clears and closes this and parent form
HotovoEnd:  My.Forms.fZadaniVydaje.Dispose()
            My.Forms.fZadaniVydaje.Close()
            Me.Close()
        Else
            MsgBox("Je třeba vyplnit všechny položky!", MsgBoxStyle.Exclamation, "Chyba!")
        End If
    End Sub
End Class